package com.lf.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lf.DAO.studentDAO;
import com.lf.DAO.teacherDAO;
import com.lf.entity.mapper.teacher_Mapper;
import com.lf.entity.shiti.students;
import com.lf.entity.shiti.teacher;
import com.lf.impl.stu_impl;
import com.lf.impl.tea_impl;



/**
 * Servlet implementation class teacher_Servlet
 */
@WebServlet("/teacher_Servlet")
public class teacher_Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public teacher_Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String action = request.getParameter("action");
		if(action==null){
			findAll(request,response);
			
		}
		switch (action) {
		case "findAll":
			findAll(request,response);
			break;
		case "find_name":
			find_name(request,response);
			break;
		case"add_tea":
			add_tea(request,response);
			break;
		case"update_tea":
			update_tea(request,response);
			break;
		case"delect_tea":
			delect_tea(request,response);
			break;
		case"login":
			login(request,response);
			break;
		default:
			break;
		}
		
	}

	
	private void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException{
		teacherDAO tDao = new tea_impl();
		List<teacher> list = tDao.findAll();
		request.getSession().setAttribute("list", list);
		response.sendRedirect("test01.jsp");
	}
	
	
	private void find_name(HttpServletRequest request, HttpServletResponse response) throws IOException{

		String t_name = request.getParameter("t_name");
		t_name = URLEncoder.encode(t_name, "ISO-8859-1");
		t_name = URLDecoder.decode(t_name, "UTF-8");
		System.out.println(t_name);
		teacherDAO tDao = new tea_impl();
		List<teacher> name_list = tDao.find_name(t_name);
		request.getSession().setAttribute("name_list", name_list);
		response.sendRedirect("test02.jsp");

	}
	
	private void add_tea(HttpServletRequest request, HttpServletResponse response) throws IOException{
		System.out.print("asda");
		String t_id = request.getParameter("t_id");
		String t_name = request.getParameter("t_name");
		String t_password = request.getParameter("t_password");
		t_id = URLEncoder.encode(t_id, "ISO-8859-1");
		t_id = URLDecoder.decode(t_id, "UTF-8");
		t_name = URLEncoder.encode(t_name, "ISO-8859-1");
		t_name = URLDecoder.decode(t_name, "UTF-8");
		t_password = URLEncoder.encode(t_password, "ISO-8859-1");
		t_password = URLDecoder.decode(t_password, "UTF-8");
		System.out.println(t_id);
		System.out.println(t_name);
		System.out.println(t_password);
		teacherDAO tDao = new tea_impl();
		int count = tDao.add_tea(t_id, t_name, t_password);
		if (count>0) {
			System.out.print("成功");
		} else {
			System.out.print("失败");
		}
		
		response.sendRedirect("test01.jsp");
	}
	
	private void update_tea(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String t_id = request.getParameter("t_id");
		String t_name = request.getParameter("t_name");
		String t_password = request.getParameter("t_password");
		t_id = URLEncoder.encode(t_id, "ISO-8859-1");
		t_id = URLDecoder.decode(t_id, "UTF-8");
		t_name = URLEncoder.encode(t_name, "ISO-8859-1");
		t_name = URLDecoder.decode(t_name, "UTF-8");
		t_password = URLEncoder.encode(t_password, "ISO-8859-1");
		t_password = URLDecoder.decode(t_password, "UTF-8");
		System.out.println("进入");
		System.out.println(t_name);
		System.out.println(t_id);
		System.out.println(t_password);
		teacherDAO tDao = new tea_impl();
		int count = tDao.update_tea(t_name, t_password,t_id);
		if (count>0) {
			System.out.print("成功");
		} else {
			System.out.print("失败");
		}
		
		response.sendRedirect("test.jsp");
		
	}
	
	private void delect_tea(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String t_id = request.getParameter("t_id");
		t_id = URLEncoder.encode(t_id, "ISO-8859-1");
		t_id = URLDecoder.decode(t_id, "UTF-8");
		
		System.out.println(t_id);
		teacherDAO tDao = new tea_impl();
		int count = tDao.delect_tea(t_id);
		if (count>0) {
			System.out.print("成功");
		} else {
			System.out.print("失败");
		}
		
		response.sendRedirect("test01.jsp");
	}
	
	/**
	 * 老师登录
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		String t_id = request.getParameter("user");
		String password = request.getParameter("password");
		teacherDAO tDao = new tea_impl();
		List<teacher> list = tDao.login(t_id);
		PrintWriter out = response.getWriter();
		if(list.size() == 0){
			System.out.println("账号不存在");
			out.print("<script>alert('账号不存在'); location.href='Login.jsp'</script>");
			out.close();
		}else{
			for(teacher t : list){
				if(t.getT_password().equals(password)){
					System.out.println("登录成功");
					findAll(request, response);
				}else{
					System.out.println("密码错误");
					out.print("<script>alert('密码错误'); location.href='Login.jsp'</script>");
					out.close();
				}
			}
		}
	}
}
